use std::alloc::{GlobalAlloc, Layout, System};

pub(crate) struct BaseSystemAllocator;

impl BaseSystemAllocator {
    #[inline(always)]
    pub(crate) unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        System.alloc(layout)
    }

    #[inline(always)]
    pub(crate) unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        System.dealloc(ptr, layout)
    }
}

#[cfg(test)]
mod tests {
    use crate::consts::SEGMENT_SIZE;
    use crate::utils::system_allocator::BaseSystemAllocator;
    use std::alloc::Layout;
    #[test]
    fn test_align() {
        let layout = Layout::from_size_align(SEGMENT_SIZE, SEGMENT_SIZE).unwrap();
        let ptr = unsafe { BaseSystemAllocator.alloc(layout) };
        // 测试对齐正确
        assert_eq!(ptr as usize % SEGMENT_SIZE, 0);
        unsafe {
            BaseSystemAllocator.dealloc(ptr, layout);
        }
    }
}
